<template>
	<view class="main">
		<view class="header">
			<view class="search">
				<image src="@/static/img/searchIcon.png" mode=""></image>
				<input type="text" v-model="data.keyword" placeholder="请搜索你需要的商品" />
				<view class="btn" @tap="search()">
					搜索
				</view>
			</view>
		</view>
		<view class="con">
			<scroll-view class="scroll1" scroll-y="true">
				<view class="left">
					<ul class="list">
						<li :class="{ active: seleTab == index, next: seleTab + 1 == index, prve: seleTab - 1 == index }"
							v-for="(item, index) in classList" :key="index" @tap="seleFn(index,item.id)">
							<view class="hr"></view>
							<view class="msg">{{ item.title }}</view>
						</li>
					</ul>
				</view>
			</scroll-view>
			<scroll-view scroll-y="true" class="scroll2" @scrolltolower="scrolltolower">
				<view class="right">
					<view class="list">
						<u-empty v-if="goodsList.length == 0 && isShow" mode="data"
							icon="http://cdn.uviewui.com/uview/empty/data.png" text="暂无商品"></u-empty>
						<view class="item" v-for="(item, index) in goodsList" :key="index"
							@tap="routerTo(`/pages/pageB/detail/detail?id=${item.id}`)">
							<view class="imgBox">
								<easy-loadimage class="image" :scroll-top="scrollTop"
									:image-src="item.image"></easy-loadimage>
								<!-- <image :src="item.image" mode=""></image> -->
							</view>
							<view class="txt">
								<view class="name">{{item.name}}</view>
								<view class="msg">{{item.intro}}</view>
								<view class="price">￥{{item.member_price ? item.member_price : item.original_price}}</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<userTabbar seleTabbar="1"></userTabbar>
	</view>
</template>

<script>
	import {
		getCates,
		indexList
	} from '@/api/user.js';
	import userTabbar from '../../components/userTabbar.vue';
	export default {
		components: {
			userTabbar
		},
		data() {
			return {
				isShow: false,
				seleTab: 0,
				classList: [],
				data: {
					page_size: 10,
					field: '',
					category_id: '2',
					page: 1,
					keyword: ''
				},
				goodsList: [],
				last_page: '',
				scrollTop:0
			};
		},
		onLoad() {
			this.classInfo();
		},
		onReachBottom() {

		},
		onPageScroll({
			scrollTop
		}) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			this.scrollTop = scrollTop;
		},
		methods: {
			scrolltolower() {
				if (this.data.page == this.last_page) {
					uni.showToast({
						title: '已经是最后一页了',
						icon: 'none'
					})
				} else {
					this.data.page++
					this.getList()
				}
			},
			classInfo() {
				getCates().then((res) => {
					console.log(res)
					for(let i = 0; i < res.data.length; i++){
						if (res.data[i].is_common != 0) {
							this.data.category_id = res.data[i].id
							break
						}
					}
					// res.data.forEach((item, index) => {
					// 	if (item.is_common != 0) {
					// 		this.data.category_id = item.id
					// 		break
					// 	}
					// })
					res.data.forEach((item, index) => {
						if (item.is_common != 0) {
							this.classList.push(item)
						}
					})
					this.getList()
				});
			},
			async getList() {
				this.isShow = false
				let res = await indexList(this.data);
				if (this.data.page == 1) {
					this.goodsList = []
				}
				res.data.data.forEach(item => {
					this.goodsList.push(item)
				})
				this.last_page = res.data.last_page
				this.isShow = true
			},
			seleFn(i, id) {
				this.seleTab = i
				this.data.page = 1
				this.data.category_id = id
				// if (i != 0) {
				// 	this.data.field = ''
				// } else {
				// 	this.data.field = 'is_recom'
				// }
				this.getList()
			},
			search() {
				this.data.page = 1
				this.getList()
			},
			routerTo(url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	};
</script>

<style lang="scss" scoped>
	.main {
		min-height: calc(100vh - 50px);
		display: flex;
		flex-direction: column;

		.header {
			padding: 74px 15px 24px;
			background-color: #fff;
			box-shadow: 0px 1px 6px 0px rgba(0, 0, 0, 0.05);
			border-radius: 0px 0px 20px 20px;
			margin-bottom: 10px;

			.search {
				width: 100%;
				border-radius: 100px;
				background-color: #f6f7f9;
				display: flex;
				align-items: center;
				padding: 10px 12px;
				margin-bottom: 25px;

				image {
					width: 12px;
					height: 12px;
					margin-right: 7px;
				}

				input {
					flex: 1;
				}

				.btn {
					padding: 3px 10px;
					background-color: #0A84FF;
					font-size: 12px;
					border-radius: 100px;
					color: #fff;
				}
			}
		}

		.con {
			display: flex;

			.scroll1 {
				width: 25%;
				max-height: calc(100vh - 176px);
			}
			.scroll2{
				width: 75%;
				max-height: calc(100vh - 176px);
			}

			.left {
				background-color: #f6f7f9;

				.list {
					width: 100%;
					height: 100%;
					padding-bottom: 50px;

					li {
						padding: 10px 0 10px 5px;
						display: flex;
						align-items: center;

						.hr {
							height: 20px;
							width: 4px;
							// background-color: #0A84FF;
							border-radius: 100px;
							margin-right: 10px;
						}

						.msg {
							color: #232323;
						}
					}

					.active {
						background-color: #fff;

						.hr {
							background-color: #0a84ff;
						}

						.msg {
							font-weight: 700;
							color: #232323;
						}
					}

					.next {
						border-radius: 0 19px 0 0;
					}

					.prve {
						border-radius: 0 0 19px 0;
					}
				}
			}

			.right {
				padding: 10px 30px 0 15px;
				// max-height: 100%;

				.list {
					padding-bottom: 50px;

					.item {
						display: flex;
						margin-bottom: 10px;

						.imgBox {
							.image {
								width: 80px;
								height: 60px;
								border-radius: 3px;
								margin-right: 20px;
							}
						}

						.txt {
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							height: 60px;

							.name {
								font-size: 12px;
								overflow: hidden;
								text-overflow: ellipsis;
								-webkit-line-clamp: 1;
								display: -webkit-box;
								-webkit-box-orient: vertical;
								font-weight: 700;
							}

							.msg {
								font-size: 11px;
								color: #666666;
							}

							.price {
								color: #e1222a;
								font-size: 15px;
							}
						}
					}
				}
			}
		}

	}
</style>